package br.com.orion.sga.model.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import br.com.orion.sga.constants.Constants;
import br.com.orion.sga.exception.DaoException;
import br.com.orion.sga.model.entities.Aluno;
import br.com.orion.sga.model.entities.PeriodoLiberacao;

@Transactional
@Repository("alunoDao")
public class AlunoDao extends GenericDao<Aluno, Long>{

	public Logger logger = Logger.getLogger("sga");
	
	public Aluno getByCPF(String cpf) throws DaoException{
		
		logger.info("search by CPF");
		logger.debug("debug");
		
		Query q = entityManager.createQuery("from Aluno a WHERE a.cpf = :cpf");
		q.setParameter("cpf", cpf);
		try {
			return (Aluno) q.getSingleResult();
		} catch (NoResultException e) {
			throw new DaoException("Nenhum registro foi localizado!");
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PeriodoLiberacao> getAlunosForReportPayment(Date initialDate, Date endDate){
		StringBuffer query = new StringBuffer();
		
		query.append("from PeriodoLiberacao p ");
		query.append("where p.status = :status and ");
		query.append("p.periodo between :initialDate and :endDate ");
		
		Query q = entityManager.createQuery(query.toString());
		q.setParameter("status", Constants.alunoLiberado);
		q.setParameter("initialDate", initialDate);
		q.setParameter("endDate", endDate);
		
		return q.getResultList();
	}
}
